import java.util.Stack;

/**
 * 括号匹配
 */
public class Test03 {
    public boolean isVaild(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (ch == '(' || ch == '{' || ch == '[') {
                stack.push(ch);
            }else {
                //遇到了右括号
                if (stack.empty()) {
                    return false;
                }
                char ch2 = stack.peek();//左括号
                if (ch == '}' && ch2 == '{' ||ch == ')' && ch2 == '('|| ch == ']' && ch2 == '[') {
                    stack.pop();
                }else {
                    return false;//不匹配
                }
            }
        }
        if (!stack.empty()) {
            return false;
        }
        return true;
    }
}
